Porting Perl to C++ `print "\x{2501}" x 12;`
Posted
by
jippie
on Stack Overflow
See other posts from Stack Overflow
or by jippie
Published on 2013-11-09T17:49:17Z
Indexed on
2013/11/10
21:55 UTC
Read the original article
Hit count: 187
I am porting a program from Perl to C++ as a learning objective. I arrived at a routine that draws a table with commands like the following:
Perl: print "\x{2501}" x 12;
And it draws 12 times a '?' ("box drawings heavy horizontal").
Now I figured out part of the problem already:
Perl: \x{}, \x00 Hexadecimal escape sequence;
C++: \unnnn
To print a single Unicode character:
C++: printf( "\u250f\n" );
But does C++ have a smart equivalent for the 'x' operator or would it come down to a for loop?
UPDATE Let me include the full source code I am trying to compile with the proposed solution. The compiler does throw an errors:
g++ -Wall -Werror project.cpp -o project
project.cpp: In function ‘int main(int, char**)’:
project.cpp:38:3: error: ‘string’ is not a member of ‘std’
project.cpp:38:15: error: expected ‘;’ before ‘s’
project.cpp:39:3: error: ‘cout’ is not a member of ‘std’
project.cpp:39:16: error: ‘s’ was not declared in this scope
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
{
fprintf( stderr , "usage: %s matrix\n", argv[0] );
exit( 2 );
} else {
//std::string s(12, "\u250f" );
std::string s(12, "u" );
std::cout << s;
}
}
© Stack Overflow or respective owner